package com.example.tyz1201.myaccount.Activity.Activity.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v7.widget.Toolbar;

import com.example.tyz1201.myaccount.Activity.Activity.Adapter.AssumeAdapter;
import com.example.tyz1201.myaccount.Activity.Activity.Model.AssumeModel;
import com.example.tyz1201.myaccount.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by tyz1201 on 2016/4/8.
 */
public class ReadActivity extends AppCompatActivity {

    Toolbar toolbar_over;
    List<AssumeModel> messages = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
    AssumeAdapter assumeAdapter;
    ListView lv_read;
    private float todoMoney = 0;
    private String choosedName, assumeData;
    private long createTime = 0;
    AssumeModel assumeModel;
    private String createTimeStr, tips, str;
    private int choosedAssumeTypeId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);

        toolbar_over = (Toolbar) findViewById(R.id.toolbar_over);
        toolbar_over.setTitle("MyAccount");
        toolbar_over.setTitleTextColor(getResources().getColor(R.color.Black));
        setSupportActionBar(toolbar_over);

        lv_read = (ListView) findViewById(R.id.lv_read);

        assumeAdapter = new AssumeAdapter(messages, ReadActivity.this);
        lv_read.setAdapter(assumeAdapter);

        lv_read.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                choosedName = messages.get(position).getChoosedName();
                choosedAssumeTypeId = messages.get(position).getChoosedAssumeTypeId();
                tips = messages.get(position).getTips();
                todoMoney = messages.get(position).getTodoMoney();
                createTime = messages.get(position).getCreateTime();
                Log.d("err", choosedName);

                Date createDate = new Date(createTime);
                SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
                createTimeStr = sf.format(createDate);

                if (choosedAssumeTypeId == -1) {
                    str = "支出";
                } else if (choosedAssumeTypeId == 1) {
                    str = "收入";
                }

                AlertDialog alertDialog = new AlertDialog.Builder(ReadActivity.this)
                        .setTitle(str + ":" + todoMoney)
                        .setMessage(choosedName + ":" + createTimeStr + "\n" + "备注:" + tips)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .create();
                alertDialog.show();
            }
        });

        lv_read.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                AlertDialog alertDialog = new AlertDialog.Builder(ReadActivity.this)
                        .setTitle("是否删除？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteItemMessages(messages.get(position));
                                reloadData();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .create();
                alertDialog.show();
                return true;
            }
        });
    }


    private void initMessages() {

        SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
        map = (Map) sharedPreferences.getAll();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            assumeData = map.get(key);
            assumeModel = AssumeModel.backToAssumeModel(assumeData);

            messages.add(assumeModel);
        }
    }

    private void sortMessages() {
        for (int i = 0; i < messages.size(); i++) {
//            Log.d("error", "i:" + i);
            AssumeModel formerAssumeModel, latterAssumeModel, midAssumeModel;
            Long ctTime, ctTime1;

            for (int j = i + 1; j < messages.size(); j++) {
//                Log.d("error", "j:" + j);
                formerAssumeModel = messages.get(i);
                latterAssumeModel = messages.get(j);

                ctTime = formerAssumeModel.getCreateTime();
                ctTime1 = latterAssumeModel.getCreateTime();
                if (ctTime > ctTime1) {
                    midAssumeModel = messages.get(i);
                    messages.set(i, messages.get(j));
                    messages.set(j, midAssumeModel);
                }
            }
        }
//        printMessages();
    }

//    void printMessages() {
//        for (AssumeModel assumeModel : messages) {
//            Log.d("error", assumeModel.toString());
//        }
//    }

    private void deleteItemMessages(AssumeModel assumeModel) {
        SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.remove(assumeModel.getCreateTime() + "");
        editor.commit();
    }

    @Override
    protected void onResume() {
        reloadData();
        super.onResume();
    }

    private void reloadData() {
        messages.clear();
        initMessages();
        sortMessages();
        assumeAdapter.notifyDataSetChanged();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_read, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.menu_total) {
            reloadData();
        }

        if (id == R.id.menu_breakfast) {
            menuDataPrepare();
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                assumeData = map.get(key);
                assumeModel = AssumeModel.backToAssumeModel(assumeData);

                choosedName = assumeModel.getChoosedName();

                if ("早餐".equals(choosedName)) {
                    messages.add(assumeModel);
                }
            }
            assumeAdapter.notifyDataSetChanged();

        }

        if (id == R.id.menu_lunch) {
            menuDataPrepare();
            Set<String> keySet = map.keySet();

            for (String key : keySet) {
                assumeData = map.get(key);
                assumeModel = AssumeModel.backToAssumeModel(assumeData);

                choosedName = assumeModel.getChoosedName();

                if ("午餐".equals(choosedName)) {
                    messages.add(assumeModel);
                }
            }
            assumeAdapter.notifyDataSetChanged();

        }

        if (id == R.id.menu_dinner) {
            menuDataPrepare();
            Set<String> keySet = map.keySet();

            for (String key : keySet) {
                assumeData = map.get(key);
                assumeModel = AssumeModel.backToAssumeModel(assumeData);

                choosedName = assumeModel.getChoosedName();

                if ("晚餐".equals(choosedName)) {
                    messages.add(assumeModel);
                }
            }
            assumeAdapter.notifyDataSetChanged();

        }

        if (id == R.id.menu_others) {
            menuDataPrepare();
            Set<String> keySet = map.keySet();

            for (String key : keySet) {
                assumeData = map.get(key);
                assumeModel = AssumeModel.backToAssumeModel(assumeData);

                choosedName = assumeModel.getChoosedName();

                if ("其他".equals(choosedName)) {
                    messages.add(assumeModel);
                }
            }
            assumeAdapter.notifyDataSetChanged();

        }

        if (id == R.id.menu_outcome) {
            menuDataPrepare();
            Set<String> keySet = map.keySet();

            for (String key : keySet) {
                assumeData = map.get(key);
                assumeModel = AssumeModel.backToAssumeModel(assumeData);

                choosedAssumeTypeId = assumeModel.getChoosedAssumeTypeId();

                if (choosedAssumeTypeId == -1) {
                    messages.add(assumeModel);
                }
            }
            assumeAdapter.notifyDataSetChanged();

        }
        if (id == R.id.menu_income) {
            menuDataPrepare();
            Set<String> keySet = map.keySet();

            for (String key : keySet) {
                assumeData = map.get(key);
                assumeModel = AssumeModel.backToAssumeModel(assumeData);

                choosedAssumeTypeId = assumeModel.getChoosedAssumeTypeId();

                if (choosedAssumeTypeId == 1) {
                    messages.add(assumeModel);
                }
            }
            assumeAdapter.notifyDataSetChanged();

        }

        return super.onOptionsItemSelected(item);
    }

    private void menuDataPrepare() {
        messages.clear();
        SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
        map = (Map) sharedPreferences.getAll();
    }

}

